using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

namespace Tyng.Web.UI.YUI
{
    public class Submenu : IStateManager
    {
        bool _isTrackingViewState;
        StateBag _viewState;
        MenuItemCollection _items = new MenuItemCollection();

        //TODO: enhance this..., give it a set, make it serialize in the designer, etc.
        [JsonIdentifier("submenualignment"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Alignment SubmenuAlignment
        {
            get
            {
                return new Alignment(Corner.TopLeft, Corner.TopRight);
            }
        }

        [JsonIdentifier("position"), DefaultValue(MenuPosition.Dynamic)]
        public MenuPosition Position
        {
            get
            {
                object o = ViewState["Position"];
                if (o == null)
                    return MenuPosition.Dynamic;

                return (MenuPosition)o;
            }
            set
            {
                ViewState["Position"] = value;
            }
        }

        [JsonIdentifier("id")]
        public string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }

        [JsonIdentifier("lazyload"), DefaultValue(false)]
        public bool LazyLoad
        {
            get
            {
                object o = ViewState["LazyLoad"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["LazyLoad"] = value;
            }
        }

        [JsonIdentifier("autosubmenudisplay"), DefaultValue(true)]
        public bool AutoSubmenuDisplay
        {
            get
            {
                object o = ViewState["AutoSubmenuDisplay"];
                if (o == null)
                    return true;

                return (bool)o;
            }
            set
            {
                ViewState["AutoSubmenuDisplay"] = value;
            }
        }

        [JsonIdentifier("clicktohide"), DefaultValue(true)]
        public bool ClickToHide
        {
            get
            {
                object o = ViewState["ClickToHide"];
                if (o == null)
                    return true;

                return (bool)o;
            }
            set
            {
                ViewState["ClickToHide"] = value;
            }
        }

        [DefaultValue(Menu.DefaultHideDelay), JsonIdentifier("hidedelay")]
        public int HideDelay
        {
            get
            {
                return Menu.DefaultHideDelay;
            }
        }

        [DefaultValue(Menu.DefaultShowDelay), JsonIdentifier("showdelay")]
        public int ShowDelay
        {
            get
            {
                return Menu.DefaultShowDelay;
            }
        }

        [JsonIdentifier("itemdata")]
        public MenuItemCollection ItemData
        {
            get
            {
                return _items;
            }
        }

        #region Viewstate
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        protected StateBag ViewState
        {
            get
            {
                if (this._viewState == null)
                {
                    this._viewState = new StateBag(false);
                    if (this._isTrackingViewState)
                    {
                        ((IStateManager)this._viewState).TrackViewState();
                    }
                }
                return this._viewState;
            }
        }

        protected internal virtual void LoadViewState(object state)
        {
            if (state != null)
            {
                Pair p = (Pair)state;

                ((IStateManager)ViewState).LoadViewState(p.First);
                ((IStateManager)_items).LoadViewState(p.Second);
            }
        }

        protected internal virtual object SaveViewState()
        {
            Pair p = new Pair();

            if (_viewState != null)
            {
                p.First = ((IStateManager)_viewState).SaveViewState();
            }
            p.Second = ((IStateManager)_items).SaveViewState();

            return p;
        }

        protected internal virtual void TrackViewState()
        {
            this._isTrackingViewState = true;
            if (_viewState != null)
            {
                ((IStateManager)_viewState).TrackViewState();
            }
            if (_items != null)
            {
                ((IStateManager)_items).TrackViewState();
            }
        }

        protected virtual bool IsTrackingViewState
        {
            get
            {
                return this._isTrackingViewState;
            }
        }

        protected internal void SetDirty()
        {
            if (this._viewState != null)
                this._viewState.SetDirty(true);

            if (_items != null)
                _items.SetDirty();
        }

#endregion

        #region IStateManager Members

        bool IStateManager.IsTrackingViewState
        {
            get { return IsTrackingViewState; }
        }

        void IStateManager.LoadViewState(object state)
        {
            LoadViewState(state);
        }

        object IStateManager.SaveViewState()
        {
            return SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            TrackViewState();
        }

        #endregion
    }
}
